<?php 

class Event{

	const startSubmissionBorder = 3600;
	const endSubmissionBorder = 300;

	function insertNewEvent($eventTypeID){
		$ret = array();
		$DB = new DB();
		$con = $DB->conDB();
		$ret['debug'] .= "Start insertNewEvent <br>\n";

		if($eventTypeID > 0){

			$EventType = new EventType();
			$retData = $EventType->getEventTypeData($eventTypeID);
			$data = $retData['data'];

			// next start timestamp berechnen
			$retTimeData = $EventType->calculateNextStartTimestamp($data['StartTime'], $data['StartDay']);
			$startTimestamp = $retTimeData['data'];

			// Start und End Submission zum Event berechnen
			$startSub = $startTimestamp-$this::startSubmissionBorder;
			$endSub = $startTimestamp-$this::endSubmissionBorder;

			$insertArray = array();
			$insertArray['EventTypeID'] = (int) $eventTypeID;
			$insertArray['StartTimestamp'] = (int) $startTimestamp;
			$insertArray['EndTimestamp'] = (int) 0;
			$insertArray['StartSubmissionTimestamp'] = (int) $startSub;
			$insertArray['EndSubmissionTimestamp'] = (int) $endSub;
			$insertArray['RegionID'] = (int) $data['RegionID'];

			$retIns = $DB->insert("Events", $insertArray);

			$ret['status'] = $retIns;
		}
		else{
			$ret['status'] = "eventTypeID = 0";
		}

		$ret['debug'] .= "End insertNewEvent <br>\n";

		return $ret;
	}

	function getNextEvent(){
		$ret = array();
		$DB = new DB();
		$con = $DB->conDB();
		$ret['debug'] .= "Start getNextEvent <br>\n";

		$sql = "SELECT e.*, et.*, 
					mm.Name as MatchModeName, mm.Shortcut as MMShortcut, 
					r.Name as RegionName, r.Shortcut as RShortcut,
					tt.Name as TTName, tt.Shortcut as TTShortcut
				FROM `Events` e JOIN `EventTypes` et ON e.EventTypeID = et.EventTypeID
					JOIN MatchMode mm ON mm.MatchModeID = et.MatchModeID
					JOIN Region r ON r.RegionID = et.RegionID
					JOIN TournamentTypes tt ON tt.TournamentTypeID = et.TournamentTypeID
				ORDER BY StartTimestamp ASC
				LIMIT 1
				";
		$ret['debug'] .= p($sql,1);
		$data = $DB->select($sql);

		$ret['data']  =$data;
		$ret['status'] = true;

		$ret['debug'] .= "End getNextEvent <br>\n";

		return $ret;
	}

}

?>